﻿﻿//======================================================================
//
//        Copyright : Zhengzhou Strawberry Computer Technology Co.,LTD.
//        All rights reserved
//        
//        Application:NFinal MVC framework
//        Filename :CookieManager.cs
//        Description :Cookie管理类
//
//        created by Lucas at  2015-6-30`
//     
//        WebSite:http://www.nfinal.com
//
//======================================================================
using System;
using System.Collections.Generic;
using System.Web;

namespace ${project}.${app}.Code.Data
{
    public class CookieManager
    {
        private HttpContext _context;
		private Microsoft.Owin.IOwinContext _webContext;
        private CookieInfo _cookieInfo;

        private NFinal.ServerType _serverType = NFinal.ServerType.UnKnown;
        public CookieManager()
        {
            this._serverType = NFinal.ServerType.IsStatic;
            _cookieInfo = new CookieInfo();
        }
		public CookieManager(Microsoft.Owin.IOwinContext context)
        {
            this._webContext = context;
            this._serverType = NFinal.ServerType.MicrosoftOwin;
            _cookieInfo = new CookieInfo(); 
        }
        public CookieManager(HttpContext context)
        {
            this._serverType = NFinal.ServerType.AspNET;
            this._context = context;
			_cookieInfo = new CookieInfo();
        }
		public string session_id
        {
            get
            {
                if (this._serverType == NFinal.ServerType.AspNET)
                {
                    if (_context.Request.Cookies["session_id"] != null)
                    {
                        _cookieInfo.session_id = _context.Request.Cookies["session_id"].Value;
                    }
                    else
                    {
                        _cookieInfo.session_id = Guid.NewGuid().ToString("N");
                        HttpCookie cookie = new HttpCookie("session_id", _cookieInfo.session_id);
                        _context.Response.Cookies.Add(cookie);
                    }
                }
                else if (this._serverType == NFinal.ServerType.MicrosoftOwin)
                {
                    if (_webContext.Request.Cookies["session_id"] != null)
                    {
                        _cookieInfo.session_id = _webContext.Request.Cookies["session_id"];
                    }
                    else
                    {
                        _cookieInfo.session_id = Guid.NewGuid().ToString("N");
                        _webContext.Response.Cookies.Append("session_id", _cookieInfo.session_id);
                    }
                }
				return _cookieInfo.session_id;
            }
            set
            {
                _cookieInfo.session_id = value;
                if (this._serverType == NFinal.ServerType.AspNET)
                {
                    if (_context.Response.Cookies["session_id"] == null)
                    {
                        HttpCookie cookie = new HttpCookie("session_id", value);
                        _context.Response.Cookies.Add(cookie);
                    }
                    else
                    {
                        _context.Request.Cookies["session_id"].Value = value;
                    }
                }
                else if (this._serverType == NFinal.ServerType.MicrosoftOwin)
                {
                    if (_webContext.Request.Cookies["session_id"] != null)
                    {
                        _webContext.Response.Cookies.Delete("session_id");
                    }
                    _webContext.Response.Cookies.Append("session_id", value);
                }
            }
        }
        public string vcheck
        {
            get
            {
                if (this._serverType == NFinal.ServerType.AspNET)
                {
                    if (_context.Request.Cookies["vcheck"] != null)
                    {
                        _cookieInfo.vcheck = _context.Request.Cookies["vcheck"].Value;
                    }
                }
                else if (this._serverType == NFinal.ServerType.MicrosoftOwin)
                {
                    if (_webContext.Request.Cookies["vcheck"] != null)
                    {
                        _cookieInfo.vcheck = _webContext.Request.Cookies["vcheck"];
                    }
                }
                return _cookieInfo.vcheck;
            }
            set
            {
                _cookieInfo.vcheck = value;
                if (this._serverType == NFinal.ServerType.AspNET)
                {
                    if (_context.Request.Cookies["vcheck"] == null)
                    {
                        HttpCookie cookie = new HttpCookie("vcheck", value);
                        _context.Response.Cookies.Add(cookie);
                    }
                    else
                    {
                        _context.Response.Cookies["vcheck"].Value = value;
                    }
                }
                else if (this._serverType == NFinal.ServerType.MicrosoftOwin)
                {
                    if (_webContext.Request.Cookies["vcheck"] != null)
                    {
                        _webContext.Response.Cookies.Delete("vcheck");
                    }
                    _webContext.Response.Cookies.Append("vcheck", value);
                }
            }
        }
        public string check
        {
            get
            {
                if (this._serverType == NFinal.ServerType.AspNET)
                {
                    if (_context.Request.Cookies["check"] != null)
                    {
                        _cookieInfo.check = _context.Request.Cookies["check"].Value;
                    }
                }
                else if (this._serverType == NFinal.ServerType.MicrosoftOwin)
                {
                    if (_webContext.Request.Cookies["check"] != null)
                    {
                        _cookieInfo.check = _webContext.Request.Cookies["check"];
                    }
                }
                return _cookieInfo.check;
            }
            set
            {
                _cookieInfo.check = value;
                if (this._serverType == NFinal.ServerType.AspNET)
                {
                    if (_context.Request.Cookies["check"] == null)
                    {
                        HttpCookie cookie = new HttpCookie("check", value);
                        _context.Response.Cookies.Add(cookie);
                    }
                    else
                    {
                        _context.Response.Cookies["check"].Value = value;
                    }
                }
                else if (this._serverType == NFinal.ServerType.MicrosoftOwin)
                {
                    if (_webContext.Request.Cookies["check"] != null)
                    {
                        _webContext.Response.Cookies.Delete("check");
                    }
                    _webContext.Response.Cookies.Append("check", value);
                }
            }
        }
    }
}